'''
Created on 01/05/2010

@author: User
'''

from Models import DTO
from Models import Song, SongVSplaylist, FriendSuggestions, Properties
from google.appengine.api import users
from google.appengine.api import mail
#import atom
#import gdata.contacts
import gdata.contacts.service




class FriendsSuggestionUtils:
   
    def getFriendsList(self):
        gd_client = gdata.contacts.service.ContactsService()
        friends_dto =[]
        feed = gd_client.GetContactsFeed()
        for entry in enumerate(feed.entry):
            # get the primary email address for the contact.
            for email in entry.email:
                if email.primary and email.primary == 'true':
                    email = email.address
                    friends_dto.append(self.to_dto(email))
        return friends_dto
    
    def getSuggestionAndUnreadNum(self):        
        total = FriendSuggestions.gql("WHERE receiver = :1", users.get_current_user().email()).count()
        #notread = FriendSuggestions.gql("WHERE reciever = :1 AND is_read = :2", users.get_current_user(), False).count()
        #return total
        #feed = FriendSuggestions.gql("WHERE reciever = 'sharabiomri@gmail.com'").fetch(1000)
        #i = 0
        #for j in feed:
            #i = i + 1
        return total
    
    def getFriendsSuggestions(self, startIndex, resultNum): #ORDER BY date DESC
        total = FriendSuggestions.gql("WHERE receiver = :1", users.get_current_user().email()).fetch(resultNum,(FriendSuggestions.gql("WHERE receiver = :1", users.get_current_user().email()).count() - startIndex - resultNum))
        #total = FriendSuggestions.all() 
        #total.filter("receiver =", users.get_current_user().email()) 
        #total.order("date")        
        suggestions_dto =[]
        for suggest in total:
            suggestions_dto.append(self.to_friendSuggestion_dto(suggest))
        return suggestions_dto
    
    def sendSuggestionToUsers(self,user_mail_array, song_id):
        song = Song.get(song_id)
        newsong = Song()        
        newsong.name = song.name
        newsong.artist = song.artist
        newsong.title = song.title
        newsong.album = song.album
        newsong.time = song.time
        newsong.duration = song.duration
        newsong.genre = song.genre
        newsong.infoStatus = song.infoStatus
        newsong.genreStatus = song.genreStatus
        newsong.path = ""
        newsong.id = song.id
        newsong.put()
                
        for i in user_mail_array:
            newsugg = FriendSuggestions()
            newsugg.receiver = users.User(i).email()
            newsugg.sender = users.get_current_user().nickname()        
            newsugg.is_read = False
            newsugg.song_id = str(newsong.key())            
            newsugg.put()
        return 1
    
    def deleteSuggestion(self,keyString):
        suggestion = FriendSuggestions.get(keyString)    	
        suggestion.delete()    	
        return 1
    
    def sendEmailToUsers(self,user_mail_array, video_id, song_id):
        for i in user_mail_array:
            check = True
            prop = Properties.gql("WHERE user = :1", i).get()
            if prop:
                if (prop.recieveEmails):
                    check = True
                else:
                    check = False
            if check:                                
                mail.send_mail(sender="youtunes.tau@gmail.com", 
                  to= i, 
                  subject="%s suggests you a song on YouTunes!" % users.get_current_user().nickname(),
                  body="""

You can listen to the song on your Youtunes and add it to your playlists:
http://youtunes-tau2010.appspot.com/

You can also listen to the song on YouTube using the following URL:
http://www.youtube.com/watch?v=%s 

""" % video_id)  
        return self.sendSuggestionToUsers(user_mail_array, song_id)

    
    def addExistingSong(self, song_id, playlist_id):
        song = Song.get(song_id)
        newsong = Song()
        newsong.name = song.name
        newsong.artist = song.artist
        newsong.title = song.title
        newsong.album = song.album
        newsong.time = song.time
        newsong.duration = song.duration
        newsong.genre = song.genre
        newsong.id = song.id 
        newsong.genreStatus = song.genreStatus
        newsong.infoStatus = song.infoStatus
        newsong.path = ""       
        newsong.put()
        new_sVSp = SongVSplaylist()
        new_sVSp.playlist_id = playlist_id #in string
        new_sVSp.song_id = str(newsong.key()) #in string
        new_sVSp.put()        
        return str(newsong.key())
         
    def to_friend_dto(self, mail):
        tmpuser = users.User(mail)        
        dto = DTO()
        dto.nickname = tmpuser.nickname()
        dto.email = mail
        return dto
    
    def to_stat_dto(self,total,notread):
        dto = DTO()
        dto.total = total
        dto.notread = notread
        return dto
    
    def to_friendSuggestion_dto(self,suggestion):
        dto = DTO()
        dto.sender = suggestion.sender
        dto.date = suggestion.date
        dto.song_id = suggestion.song_id
        song = Song.get(suggestion.song_id)
        dto.video_id = song.id
        dto.name = song.name
        dto.artist = song.artist
        dto.title = song.title
        dto.album = song.album
        dto.genre = song.genre
        dto.duration = song.duration
        dto.time = song.time
        dto.isRead = suggestion.is_read
        dto.suggestionKey = str(suggestion.key())
        return dto
        
        
    
                
        
        
        